<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">


        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>工程信息</span><span onclick="go_to_engin_xiang()" class="colorlan" style="float: right;margin-right: 12px">查看工程概况</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>工程名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=hou_gongshui','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=hou_gongshui','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">工程负责人</label>
                        <div class="layui-input-block">
                            <input type="text" id="gofuer" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>票税信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">工程采购合同金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="caifee" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">工程采购合同结算金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="caijiefee" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">工程进项票金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piaoshoufee" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">工程进项税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piaoshouratefee" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">工程销项票金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piaokaifee" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">工程销项税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piaokairatefee" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">应收取税款保证金(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="yingkoushuifee" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">已收取税款保证金(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="yikoushuifee" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">待收取税款保证金(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="daikoushuifee" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">工程累计抵扣进项税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="leigongshuifee" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>







            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>抵扣进项税额信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>本次抵扣进项税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" placeholder="明细自动合计" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">款项类型</label>
                        <div class="layui-input-block">
                            <input type="text" placeholder="请输入" value="进项税额抵扣" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">累计抵扣进项税额含本次(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="leifee" id="leifee" placeholder="明细自动合计" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>认证日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="rendt" id="rendt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>退税方式</label>
                        <div class="layui-input-block">
                            <select id="tuifang" name="tuifang" lay-filter="tuifang" ></select>
                            <p class="error" id="tuifang_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联流程</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                            <div class="choose_div" id="flowrelas" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="advanersDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预审人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="advaners"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="nexterDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="nexter"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>抵扣明细</span></div>
                        <div>
                            <button type="button" onclick="choose_engin_gong_tai_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从工程销售方选择</button>
                            <button type="button" onclick="choose_dealcom_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从往来单位选择</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>销售方名称</th>
                            <th>累计支付销售方(元)</th>
                            <th>销售方进项票累计(元)</th>
                            <th>已签采购合同金额(元)</th>
                            <th>可抵扣进项票金额(元)</th>
                            <th>已抵扣进项票价税合计(元)</th>
                            <th>已抵扣进项税额(元)</th>
                            <th>剩余可抵扣进项票金额(元)</th>
                            <th><text>*</text>本次抵扣进项票价税合计(元)</th>
                            <th>本次抵扣税额(元)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror listerror_list">提示：<span></span></div>
                </div>
            </div>
        </div>

        <div class="block-generic zjzf">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="2">
                    <div class="opt-btn-group">
                        <div><span>付款明细</span></div>
                        <div>
                            <button type="button" onclick="choose_fuer_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从往来单位选择</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>付款对象</th>
                            <th><text>*</text>支付方式</th>
                            <th><text>*</text>账户</th>
                            <th>账号</th>
                            <th>开户行</th>
                            <th><text>*</text>本次支付抵扣税额(元)</th>
                            <th>款项说明</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_fu"></tbody>
                    </table>
                    <div class="listerror listerror_fu">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    var param_enginid = getQueryVariable('enginid') || 0;

    $("#commentForm").validate({
        rules: {
            rendt: {required: true},
        },
        messages:{
            rendt: {required: "请选择"},
        }
    });


    function choose_dealcom_bank(obj){
        var domkey = $(obj).parents('tr').index();
        var name = get_choose_val('fuer');
        if(!name){
            facade.error("请先选择付款对象");return false;
        }
        openPage('../public/choose_dealcom_bank.php?page=hou_gongshui&name='+encodeURI(name)+'&domkey='+domkey,'80%','80%');
    }




    function choose_engin_gong_tai_list(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择工程名称");return false;
        }
        openPage('../public/choose_engin_gong_tai.php?page=hou_gongshui_list&choose_type=checkbox&enginid='+enginid,'80%','80%');
    }

    function choose_dealcom_list(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择工程名称");return false;
        }
        openPage('../public/choose_dealcom.php?page=hou_gongshui_list&choose_type=checkbox&enginid='+enginid,'80%','80%');
    }

    function choose_fuer_list(){
        openPage('../public/choose_dealcom.php?page=hou_gongshui_fu&choose_type=checkbox','80%','80%');
    }



    function choose_piao_shou_guan(obj){
        var domkey = $(obj).parents('tr').index();
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择工程名称");return false;
        }

        var xiaoer = $(obj).parents('tr').find('.xiaoer').val() || '';
        if(!xiaoer){
            facade.error("请先选择付款对象");return false;
        }
        openPage('piao_shou_guan.php?domkey='+domkey+'&enginid='+enginid+'&xiaoer='+encodeURI(xiaoer),'80%','80%');
    }


    var pub_incdaotype;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_add_hou_gongshui, data:{enginid:param_enginid}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                }
                if(data.isnext){
                    $("#nexterDiv").removeClass('layui-hide');
                }


                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                }



                var tuifang_str = '<option value="">请选择</option>';
                var tuifang = res.data.tuifang;
                for (var key in tuifang){
                    if(key == 1){
                        tuifang_str += '<option value="'+key+'" selected>'+tuifang[key]+'</option>';
                    }else{
                        tuifang_str += '<option value="'+key+'">'+tuifang[key]+'</option>';
                    }
                }
                $("#tuifang").html(tuifang_str);
                $(".tuifangxian").removeClass('layui-hide');

                pub_incdaotype = res.data.incdaotype;

                short_table_opt_func('', 1);
                short_table_opt_func('', 2);


                public_upload_file_load('upload_files', res.data.files);
                render_from();
                initParseMoney();
            }
        });

        form.on('select(tuifang)', function(data){
            if(data.value == ''){
                $('#tuifang_error').show().text('请选择');
                $('#tuifang_error').parent().find('input').addClass('select_error');
            }else{
                $('#tuifang_error').hide();
                $('#tuifang_error').parent().find('input').removeClass('select_error');
            }
            if(data.value == 1){
                $(".tuifangxian").removeClass('layui-hide');
                $(".zjzf").removeClass('layui-hide');
            }else{
                $(".zjzf").addClass('layui-hide');
                $(".tuifangxian").addClass('layui-hide');
            }
        });

        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');

            data.field.enginid = get_choose_val('enginid');
            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();

                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }

                if(data.field.tuifang == ''){
                    $('#tuifang_error').show().text('请选择');
                    $('#tuifang_error').parent().find('input').addClass('select_error');
                    validRes = false;
                }else{
                    $('#tuifang_error').hide();
                    $('#tuifang_error').parent().find('input').removeClass('select_error');
                }


                if(!validRes){return false;}
            }



            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var xiaoer = $(this).find('.xiaoer').val();
                    var shengdifee = $(this).find('.shengdifee').val();
                    var money = $(this).find('.money').val();
                    var ratefee = $(this).find('.ratefee').val();
                    var explain = $(this).find('.explain').val();
                    var difeeobj = $(this).find('.difeeobj').text();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!xiaoer){
                            throw new Error('第'+index+'行，请选择销售方名称');
                        }
                        if(!money){
                            throw new Error('第'+index+'行，请选择本次抵扣进项票价税合计');
                        }
                        if(parseFloat(money) > parseFloat(shengdifee)){
                            throw new Error('第'+index+'行，本次抵扣进项票价税合计已超过剩余可抵扣进项票金额');
                        }
                    }


                    list.push({
                        xiaoer:xiaoer,
                        money:money,
                        ratefee:ratefee,
                        explain:explain,
                        difeeobj:difeeobj,
                    });
                });
            } catch(e) {
                $(".listerror_list").find('span').text(e.message);
                $(".listerror_list").show();
                return false;
            }
            $(".listerror_list").hide();
            data.field.list = list;


            var fuArr = [''];
            try {
                $('#list_fu tr').each(function (i) {
                    var fuer = $(this).find('.fuer').val();
                    var incdaotype = $(this).find('.incdaotype').find('input').val();
                    var bankzhang = $(this).find('.bankzhang').val();
                    var banknum = $(this).find('.banknum').val();
                    var bankname = $(this).find('.bankname').val();
                    var money = $(this).find('.money').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!fuer){
                            throw new Error('第'+index+'行，请选择付款对象');
                        }
                        if(!incdaotype){
                            throw new Error('第'+index+'行，请选择支付方式');
                        }
                        if(!bankzhang){
                            throw new Error('第'+index+'行，请选择账户');
                        }
                        if(money == ''){
                            throw new Error('第'+index+'行，请输入本次税款支付金额');
                        }
                    }

                    fuArr.push({
                        fuer:fuer,
                        incdaotype:incdaotype,
                        bankzhang:bankzhang,
                        banknum:banknum,
                        bankname:bankname,
                        money:money,
                        explain:explain
                    });

                });
            } catch(e) {
                $(".listerror_fu").find('span').text(e.message);
                $(".listerror_fu").show();
                return false;
            }
            $(".listerror_fu").hide();
            data.field.fu = fuArr;



            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            public_flow_submit(data.field, ask_flowdata_add_hou_gongshui, ask_flowdata_edit_hou_gongshui, $(this).attr('but'));

            return false;
        });

    });



    $('#list_list').on('input propertychange','.ratefee',function () {
        jisuan_ratefee_all();
    });

    function jisuan_ratefee_all() {
        var allratefee = 0;

        $('#list_list tr').each(function (i) {
            var ratefee = parseValidNum($(this).find('.ratefee').val());
            allratefee = parseFloat(allratefee) + parseFloat(ratefee);
        });

        $("#money").val(parseFormatNum(allratefee,2));

        var leigongshuifee = parseValidNum($("#leigongshuifee").val() || 0) || 0;
        var leifee = parseFloat(allratefee) + parseFloat(leigongshuifee);

        $("#leifee").val(parseFormatNum(leifee,2));
    }


    function update_refresh_row_func() {
        render_date();
        render_from();
        jisuan_ratefee_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        $.each($("#list_fu").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }




    function go_to_engin_xiang(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择工程名称");return false;
        }
        openPage('../engin/xiang.php?id='+enginid);
    }
</script>
</body>
</html>
